//
//  EmotionPageView.m
//  wanzhaoIM
//
//  Created by wanzhao on 15/8/20.
//  Copyright (c) 2015年 liuyuantao. All rights reserved.
//

#import "EmotionPageView.h"
#import "EmotionScroll.h"
#import "MicroDef.h"
#import "UIUtil.h"

@interface EmotionPageView ()<UIScrollViewDelegate, EmotionScrollDelegate>

@property (nonatomic, strong) EmotionScroll *emoScroll;
@property (nonatomic, strong) UIPageControl *pageCon;

@end


static EmotionPageView* shared;
@implementation EmotionPageView


- (void)dealloc
{
//    NSLog(@"EmotionScroll dealloc");
}

+(EmotionPageView*)defaultEmoPageView
{
    @synchronized(self)
    {
        if(shared == nil)
        {
            shared = [[EmotionPageView alloc] init];
        }
    }
    return shared;
}



- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupSubviews];
    }
    return self;
}


- (void)setupSubviews
{
    
    _emoScroll = [EmotionScroll defaultEmoScroll];
    [_emoScroll setFrame:CGRectMake(0,0, HARDWARE_SCREEN_WIDTH, 120)];
    [_emoScroll setBackgroundColor:RGBCOLOR(237, 237, 237)];//RGBCOLOR(245, 242, 222)];
//    _emoScroll.backgroundColor = RedColor;
    _emoScroll.delegate = self;
    _emoScroll.emoDelegate = self;
    //    _emoScroll.backgroundColor = RedColor;
    [self addSubview:_emoScroll];
    
    
    _pageCon = [[UIPageControl alloc] init];
    [_pageCon setFrame:CGRectMake(0, 120, 1, 20)];
    [_pageCon setBackgroundColor:RGBCOLOR(237, 237, 237)]; //RGBCOLOR(245, 242, 222)];
    [_pageCon setCurrentPageIndicatorTintColor:[UIColor grayColor]];
    
    [_pageCon setPageIndicatorTintColor:RGBCOLOR(174, 174, 174)];
    
    //[_pageCon setPageIndicatorTintColor:[UIUtil colorWithHexString:@"#fd8584" withAlpha:1.0]];
//    [_pageCon setNumberOfPages:3];
    [_pageCon addTarget:self action:@selector(tapPageControl:) forControlEvents:UIControlEventTouchUpInside];
    
    [self addSubview:_pageCon];

}

//page的设置
- (void)setEmotionArray:(NSArray *)emotionArray
{
    _emotionArray = emotionArray;
    _emoScroll.emotionArray = emotionArray;
     NSInteger pages = [emotionArray count]%23 ? [emotionArray count]/23 + 1 : [emotionArray count]/23;
    [_pageCon setNumberOfPages:pages];
}


-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    _pageCon.currentPage = (int)_emoScroll.contentOffset.x/HARDWARE_SCREEN_WIDTH;
}

-(void)tapPageControl:(id)sender
{
    _emoScroll.contentOffset = CGPointMake(_pageCon.currentPage*HARDWARE_SCREEN_WIDTH, 0);
}

@end
